﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using EFB.Lib;
using EFB.Lib.Entity;
using EFB.Lib.Process;

namespace EFB.Web.telasMobile
{
    public partial class ProgramacaoProducaoTela : System.Web.UI.UserControl
    {
        public String TipoOperacao { get; set; }
        public String UrlAnterior { get; set; }
        public String Produto { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            CompareValidatorDataInicio.ValueToCompare = DateTime.Now.ToShortDateString();
            txtOperacaoPP.Text = this.TipoOperacao;
            if (!String.IsNullOrEmpty(Request.QueryString["sku"])) {
                txtOpPP.Text = Request.QueryString["sku"];
            }
             
            if (this.TipoOperacao.Equals("Saida"))
            {
                txtOpPP.Enabled = false;
            }
        

        }

        protected void btnLimpar_Click(object sender, EventArgs e)
        {
            txtDataPrograma.Text = "";
            txtQuantidade.Text = "";
            txtOpPP.Text = "";
    
        }

        protected void Retornar_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.UrlAnterior, true);
        }

        protected void btnRegistrar_Click(object sender, EventArgs e)
        {
            BaseProcess Process = new BaseProcess();
            try
            {

                OrdProdEntity OperacaoProd = new OrdProdEntity();
               
                OperacaoProd.skuId = txtOpPP.Text;
             
                OperacaoProd.dataPrograma = DateTime.Parse(txtDataPrograma.Text);
                OperacaoProd.qtdPrograma = double.Parse(txtQuantidade.Text);
                OperacaoProd.operacao = this.TipoOperacao;

                //  if (CheckBox1.Checked)
                    //  {
              //      OperacaoProd.simulacao = "S";
              //  }
                //  else {
                //    OperacaoProd.simulacao = "N";
                //}

                Process.DatabaseConnect();
                Process.BeginTransaction();

                OrdProdProcess OperProc = new OrdProdProcess();

                OperProc.Incluir(OperacaoProd);

                Process.CommitTransaction();
                String mensa;
                if (OperacaoProd.opId != 0)
                {
                    mensa = " OP = " + OperacaoProd.opId;
                }
                else {
                    mensa = "";
                }

                FailureText.Text = "Operação realizada com sucesso." + mensa;
                FailureText.ForeColor = System.Drawing.Color.Blue;
                FailureText.Visible = true;
                FailureContainer.Visible = true;

            }
            catch (Exception Except)
            {
                Process.RollbackTransaction();
                FailureText.Text = ContextHelper.FormatExceptionText(Except);
                FailureText.Visible = true;
                FailureContainer.Visible = true;
            }
            finally
            {
                Process.DatabaseDisconnect();
            }
        }

    }
}